﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AccountEditorViewModel.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the AccountEditorViewModel type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Windows.Client.ViewModel
{
    using System;
    using System.Diagnostics.Contracts;
    using System.Linq;
    using System.Windows.Data;
    using Business.Interfaces;
    using Common;
    using Hisaab.Common;

    using Microsoft.Practices.ServiceLocation;

    using Model;

    /// <summary>
    /// Defines the <seealso cref="Hisaab.Windows.Client.ViewModel.AccountEditorViewModel"/> type.
    /// </summary>
    public class AccountEditorViewModel : ViewModelBase
    {
        /// <summary>
        /// Save command
        /// </summary>
        public static readonly ActiveAwareCommand SaveCommand = new ActiveAwareCommand();

        /// <summary>
        /// Session manager
        /// </summary>
        private readonly ISession _session;

        /// <summary>
        /// Account manager
        /// </summary>
        private readonly IAccountManager _accountManager;

        /// <summary>
        /// The type manager
        /// </summary>
        private readonly ITypeManager _typeManager;

        /// <summary>
        /// Account types
        /// </summary>
        private CollectionView _accountTypes;

        /// <summary>
        /// Selected account
        /// </summary>
        private Account _selectedAccount;

        /// <summary>
        /// Initializes a new instance of the <see cref="AccountEditorViewModel"/> class.
        /// </summary>
        /// <param name="accountToEdit">Account to edit</param>
        public AccountEditorViewModel(Account accountToEdit)
            : this(
            ServiceLocator.Current.GetInstance<IAccountManager>(),
            ServiceLocator.Current.GetInstance<ITypeManager>(),
            ServiceLocator.Current.GetInstance<ISession>(),
            accountToEdit)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AccountEditorViewModel"/> class.
        /// </summary>
        /// <param name="accountManager">The account manager.</param>
        /// <param name="typeManager">The type manager</param>
        /// <param name="session">The sesstion</param>
        /// <param name="accountToEdit">Account to edit</param>
        public AccountEditorViewModel(IAccountManager accountManager, ITypeManager typeManager, ISession session, Account accountToEdit)
        {
            Contract.Requires<ArgumentNullException>(accountManager != null);
            Contract.Requires<ArgumentNullException>(typeManager != null);
            Contract.Requires<ArgumentNullException>(session != null);

            _accountManager = accountManager;
            _typeManager = typeManager;
            _session = session;
            IsNewAccount = accountToEdit == null;
            SelectedAccount = accountToEdit ?? new Account { InitialBalanceDate = DateTime.Today };
        }

        /// <summary>
        /// Gets or sets the selected account.
        /// </summary>
        /// <value>
        /// The selected account.
        /// </value>
        public Account SelectedAccount
        {
            get
            {
                return _selectedAccount;
            }

            set
            {
                if (value != null && _selectedAccount != value)
                {
                    _selectedAccount = value;

                    OnPropertyChanged("SelectedAccount");
                }
            }
        }

        /// <summary>
        /// Gets the Account Types.
        /// </summary>
        public CollectionView AccountTypes
        {
            get
            {
                if (_accountTypes == null)
                {
                    _accountTypes = new CollectionView(_typeManager.ListAccountTypes().Select(x => x.Map<Business.Entities.AccountType, AccountType>()));
                }

                return _accountTypes;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether [new account].
        /// </summary>
        /// <value>
        ///   <c>true</c> if [new account]; otherwise, <c>false</c>.
        /// </value>
        public bool IsNewAccount { get; set; }

        /// <summary>
        /// Saves the account.
        /// </summary>
        /// <param name="obj">The object</param>
        public void SaveAccount(object obj)
        {
            if (IsNewAccount)
            {
                SelectedAccount.UserId = _session.LoggedUser;
                _accountManager.Add(SelectedAccount.Map<Account, Business.Entities.Account>());
            }
            else
            {
                _accountManager.Update(SelectedAccount.Map<Account, Business.Entities.Account>());
            }

            DocumentManager.Instance.CloseCurrent();
        }

        /// <summary>
        /// Determines whether this instance [can save account] the specified obj.
        /// </summary>
        /// <param name="obj">The object</param>
        /// <returns>
        /// True if this instance can save account the specified obj; otherwise, false.
        /// </returns>
        public bool CanSaveAccount(object obj)
        {
            return true;
        }
    }
}